package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"house-mng-sys/models"
)

type HouseControllers struct {
	beego.Controller
}

type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c HouseControllers) Add() {
	money := c.GetString("money")
	asc := c.GetString("asc")
	name := c.GetString("name")
	phone := c.GetString("phone")
	house := models.House{
		Money: money,
		Asc:   asc,
		Name:  name,
		Phone: phone,
	}
	err := models.DB.Create(&house).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
		}
		c.ServeJSON()
	}
}

func (c HouseControllers) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	offset := (page - 1) * size
	var count int64
	query := models.DB.Model(&models.House{})
	var house []models.House
	query.Offset(offset).Limit(size).Find(&house)
	query.Count(&count)
	fxd := make(map[string]interface{})
	fxd["count"] = count
	fxd["house"] = house
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "查询成功",
		Data: fxd,
	}
	c.ServeJSON()
	return
}
